package com.zretc.blog.service;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.zretc.api.pojo.Blog;
import com.zretc.blog.dao.BlogMapper;
import com.zretc.common.entity.RespResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.websocket.SendResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static javafx.scene.input.KeyCode.R;

@Service
@Transactional(rollbackFor = Exception.class)
public class BlogService {
    //@Autowired
    //private FileService fileService;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    public RespResult addBlog(Blog blog){
        //增加博文操作
        blogMapper.addBlog(blog);
        Integer id = blog.getBloggerId();
        Map<String,Object> map= new HashMap<>();
        map.put("userId",id);
        map.put("value",7);
        rocketMQTemplate.syncSend("credit", map);
        RespResult r = new RespResult();
        return r;//fileService.upload();
    }
    public RespResult updateBlog(Blog blog){
        Integer id = blog.getId();
        Blog blog1 = blogMapper.getBlogById(id);
        blog1.setContent(blog.getContent());
        blog1.setTitle(blog.getTitle());
        blog1.setStatus(blog.getStatus());
        String key ="blogId" + String.valueOf(id);
        //stringRedisTemplate.delete(key);
        stringRedisTemplate.opsForValue().set(key,JSON.toJSONString(blog1));//修改缓存中对应的博文

        blogMapper.updateBlog(blog);
        RespResult r = new RespResult();
        return r;
       // return fileService.download();
    }
    public Blog getBlogById(Integer id){
        String key ="blogId" + String.valueOf(id);
        String blogJson = stringRedisTemplate.opsForValue().get(key);
        //System.out.println(blogJson);
        if(StringUtil.isNotBlank(blogJson)){
            Blog blog = JSON.parseObject(blogJson,Blog.class);
            return blog;
        }//缓存中存在，直接返回
        Blog blog = blogMapper.getBlogById(id);//缓存中不存在，从数据库读取
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(blog));//将从数据库读取的博文存储在缓存
        return blog;
    }
    public List<Blog> getBlogByUser(Integer bloggerId){
        /*Long size = stringRedisTemplate.opsForList().size("blogByUser");
        List<String> list_string;      //写入redis的表
        List<Blog> list;           //返回Blog对象的表
        if(size != 0){
            list_string = stringRedisTemplate.opsForList().range("blogByUser",0,size);
            list = list_string.stream().map(s -> {
                Blog blog = JSON.parseObject(s,Blog.class);
                return blog;
            }).collect(Collectors.toList());
           // System.out.println(1111);
            return list;
        }//缓存中存在，直接返回

        list = blogMapper.getBlogByUser(bloggerId);//缓存中不存在，从数据库读取
        list_string = list.stream().map(blog -> {
            return JSON.toJSONString(blog);
        }).collect(Collectors.toList());
        //System.out.println(list_string);
        stringRedisTemplate.opsForList().rightPushAll("blogByUser", list_string);
        return list;*/
       List<Blog> list = blogMapper.getBlogByUser(bloggerId);
       return list;
    }

    public ArrayList<Blog> getBlogByTitle(String info){
        ArrayList<Blog> list = blogMapper.getBlogByTitle(info);
        return list;
    }
    public ArrayList<Blog> getBlogByContent(String info){
        ArrayList<Blog> list = blogMapper.getBlogByContent(info);
        return list;
    }
    public ArrayList<Blog> getBlogByBlogger(String info){
        ArrayList<Blog> list = blogMapper.getBlogByBlogger(info);
        return list;
    }
    public ArrayList<Blog> getBlogByStatus(Integer status){
        ArrayList<Blog> list = blogMapper.getBlogByStatus(status);
        return list;
    }
    public ArrayList<Blog> getBlogByLikes(){
        ArrayList<Blog> list = blogMapper.getBlogByLikes();
        return list;
    }
//    public Integer getTotalLikes(Integer bloggerId){
//        Integer totalLikes = blogMapper.getTotalLikes(bloggerId);
//        return totalLikes;
//    }
    public RespResult setBlogStatus(Blog blog){
        blogMapper.setBlogStatus(blog);
        RespResult r = new RespResult();
        return r;
    }

    public void deleteBlog(Integer id){
        String key ="blogId" + String.valueOf(id);
        String blogJson = stringRedisTemplate.opsForValue().get(key);
        //System.out.println(blogJson);
        if(StringUtil.isNotBlank(blogJson)){
            stringRedisTemplate.delete(key);
        }
        blogMapper.deleteBlog(id);
    }
    public void likeBlog(Integer id){
        blogMapper.likeBlog(id);
    }


}
